package cn.jiangxiong.pethome.system.shop.service.impl;

import cn.jiangxiong.pethome.constants.BaseConstants;
import cn.jiangxiong.pethome.system.shop.domain.Shop;
import cn.jiangxiong.pethome.system.shop.domain.ShopRo;
import cn.jiangxiong.pethome.system.shop.mapper.ShopMapper;
import cn.jiangxiong.pethome.system.shop.query.ShopQuery;
import cn.jiangxiong.pethome.system.shop.service.IShopService;
import cn.jiangxiong.pethome.base.service.impl.BaseServiceImpl;
import cn.jiangxiong.pethome.system.user.domain.Employee;
import cn.jiangxiong.pethome.system.user.mapper.EmployeeMapper;
import cn.jiangxiong.pethome.utils.AuthService;
import cn.jiangxiong.pethome.utils.BusinessException;
import cn.jiangxiong.pethome.utils.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jx
 * @since 2023-01-07
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

   @Autowired
   private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private  AuthService authService;


    @Override
    public void add(Shop shop) {
        //一堆判断
        if(shop.getName() == null){
            throw new BusinessException("店铺名不能为空");
        }
        if(shop.getAddress() == null){
            throw new BusinessException("地址不能为空");
        }
        if(shop.getTel() == null){
            throw new BusinessException("联系方式不能为空");
        }

         //百度AI智能认证
        /*Boolean authentication = authService.authentication(shop.getName());
        if(!authentication){
            throw new BusinessException("你的店铺名存在违规信息");
        }*/
        if (!authService.authentication(shop.getName())){
            throw new BusinessException("你的店铺名存在违规信息");
        }
        /*try {
            //在访问数据库之前进行验证
            String param = "text"+shop.getName();
            String accessToken = AuthService.getAuth(baiduAI_AK, baiduAI_SK);
            String result = HttpUtil.post(baiduAI_text_url, accessToken,param);
            JSONObject jsonObject= JSONObject.parseObject(result);
            String conclusion = jsonObject.getString("conclusion");
            if (!"合规".equals(conclusion)){
                throw new BusinessException("你的店铺名字不符合规定");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //记录日志
        }*/


        //校验店铺是否存在
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setName(shop.getName());
        Long count = shopMapper.findByQueryCount(shopQuery);
        if(count!=0){
            throw new BusinessException("该["+shop.getName()+"]店铺名已存在");
        }
        //补充默认数据
        shop.setRegisterTime(new Date());//注册时间
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);//审核状态

//        shop.setAdminId(?);
        //获取到当前注册的管理员信息
        Employee admin = shop.getAdmin();
        //保存数据库 得到admin_id
        employeeMapper.add(admin);//当我保存  那么员工的id就有了

        shop.setAdminId(admin.getId());//将员工的id设置为管理员
        shopMapper.add(shop);   //当我保存  那么店铺的id就有了

        admin.setShopId(shop.getId());//将店铺的id设置给员工
        employeeMapper.update(admin);//将数据保存进去、

    }

    @Override
    public void update(Shop shop){

    }

    @Override
    public List<ShopRo> conuntByDate() {
        return shopMapper.conuntByDate();
    }

    @Override
    public void activation(Long shopid) {
        //修改店铺的状态
        Shop one = shopMapper.findOne(shopid);
        one.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopMapper.update(one);
        //得到管理员的id
        Long adminId = one.getAdminId();
        Employee one1 = employeeMapper.findOne(adminId);
        one1.setState(BaseConstants.Employee.STATE_NORMAL);
        employeeMapper.update(one1);
    }
}
